<?php

namespace app\admin\controller\foods;

use app\admin\controller\Controllers;
use app\admin\service\foods\FoodCategory as FoodCategoryService;
use app\admin\model\foods\FoodCategory as FoodCategoryModels;

class FoodCategory extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(FoodCategoryService $service)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\foods\FoodCategory.index');
        $list = $service->indexPaginate($get);
        $config = config('field.food_category');
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'status' => $config['status']
        ]);

        return $this->fetch('foods/food_category/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $config = config('field.food_category');
        $this->assign([
            'status' => $config['status']
        ]);
        return $this->fetch('foods/food_category/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(FoodCategoryService $service)
    {
        $post = $this->request->only(['title','sort_num','status'], 'post');
        $this->validate($post, 'app\admin\validate\foods\FoodCategory.create');
        return $service->create($post)?
            success():
            failed('新增失败');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  FoodCategoryModels $model
     * @return \think\Response
     */
    public function edit(FoodCategoryModels $model)
    {
        $config = config('field.food_category');
        $this->assign([
            'data' => $model,
            'status' => $config['status']
        ]);
        return $this->fetch('foods/food_category/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(FoodCategoryModels $model, FoodCategoryService $service)
    {
        $put = $this->request->only(['title','sort_num','status'], 'put');
        $this->validate($put, 'app\admin\validate\foods\FoodCategory.update');
        return $service->save($model, $put)?
            success():
            failed('编辑失败');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, FoodCategoryService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\foods\FoodCategory.delete');
        return $service->destory($id)?
            success():
            failed('删除失败');
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-12-09
     * @return \think\Response
     */
    public function destory(FoodCategoryService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\foods\FoodCategory.destory');
        return $service->destory($delete['ids'])?
            success():
            failed('删除失败');
    }

    /**
     * 状态修改
     *
     * @return \think\Response
     */
    public function status(FoodCategoryService $service, FoodCategoryModels $model)
    {
        return $service->status($model)?
            success():
            failed('更改失败');
    }

}
